/**
 * 
 */
package com.googlecode.xcarve.config;

import org.apache.commons.lang3.StringUtils;

import com.googlecode.xcarve.common.config.CarveConstances;
import com.googlecode.xcarve.util.ConfigHolder;
import com.googlecode.xcarve.util.IntnetAddressUtils;
import com.googlecode.xcarve.zk.ZkClient;
import com.googlecode.xcarve.zk.ZookeeperFactory;

/**
 * @author duxinyun
 * 
 */
public class Config{
	private String version;

	private String domain;
	
	private Class<?> interfaceClass;
	
	private String interfaceName;//接口
	
	private long timeStamp;
	
	private ServiceUser user;
	
	private volatile boolean exported = false;
	
	private String host;
	
	private int port;
	
	private transient ZkClient zkInstance;
	
	/**
	 * @return the version
	 */
	public String getVersion() {
		return version;
	}

	/**
	 * @param version the version to set
	 */
	public void setVersion(String version) {
		this.version = version;
	}

	/**
	 * @return the domain
	 */
	public String getDomain() {
		return domain;
	}

	/**
	 * @param domain the domain to set
	 */
	public void setDomain(String domain) {
		this.domain = domain;
	}

	public Class<?> getInterfaceClass() {
		return interfaceClass;
	}

	public void setInterfaceClass(Class<?> interfaceClass) {
		this.interfaceClass = interfaceClass;
	}

	/**
	 * @return the interfaceName
	 */
	public String getInterfaceName() {
		if(StringUtils.isBlank(this.interfaceName) && interfaceClass != null){
			setInterfaceName(interfaceClass.getName());
		}
		return this.interfaceName;
	}

	/**
	 * @param interfaceName the interfaceName to set
	 */
	protected void setInterfaceName(String interfaceName) {
		this.interfaceName = interfaceName;
	}

	/**
	 * @return the timeStamp
	 */
	public long getTimeStamp() {
		return timeStamp;
	}

	/**
	 * @param timeStamp the timeStamp to set
	 */
	public void setTimeStamp(long timeStamp) {
		this.timeStamp = timeStamp;
	}
	
	/**
	 * @return the user
	 */
	public ServiceUser getUser() {
		return user;
	}

	/**
	 * @param user the user to set
	 */
	public void setUser(ServiceUser user) {
		this.user = user;
	}
	
	public boolean isExported() {
		return exported;
	}

	public void setExported(boolean exported) {
		this.exported = exported;
	}

	public String getHost() {
		if(StringUtils.isBlank(host)){
			setHost(IntnetAddressUtils.getInstance().getHostAddress());
		}
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public int getPort() {
		if(port <= 0){
			setPort(ConfigHolder.getIntValue(CarveConstances.ZK_PORT));
		}
		return this.port;
	}

	public void setPort(int port) {
		this.port = port;
	}
 
	public String getAddress(){
		return getHost()+":"+getPort();
	}
	
	public ZkClient getZkInstance() {
		if(this.zkInstance == null){
			setZkInstance(ZookeeperFactory.getZKInstance(getDomain()));
		}
		return this.zkInstance;
	}

	public void setZkInstance(ZkClient zkInstance) {
		this.zkInstance = zkInstance;
	}

	protected String toServicePath() {
		StringBuilder path = new StringBuilder(CarveConstances.SEPTOR_PATH);
		path.append(CarveConstances.RPC_NAME).append(CarveConstances.SEPTOR_PATH)
		  .append(getDomain()).append(CarveConstances.SEPTOR_PATH)
		  .append(CarveConstances.RPC_PROVIDER_NAME).append(CarveConstances.SEPTOR_PATH)
		  .append(this.getInterfaceName());
		return path.toString();
	}

	
}
